<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\ApiBaseController;
use App\Http\Requests\SysRoleRequest;
use App\Models\SysRole;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SysRoleController extends ApiBaseController
{
    public function index(Request $request){
        $paginator = SysRole::filter($request->all())->paginate($this->pageSize);
        return success(page_format($paginator));
    }

    public function show(Request $request, SysRole $model){
        $id = $request->get('id');
        $data = $model->find($id);
        return success(['data' => $data->toArray()]);
    }

    public function create(SysRoleRequest $request){
        $params = $request->all();

        DB::transaction(function () use ($params) {
            $model = SysRole::create($params);
            $role_id = $model->id;
            $menuIds = $params['menuIds'];
            if(count($menuIds) > 0){
                foreach ($menuIds as $menu_id){
                    $add_datas[] = [
                        'menu_id' => $menu_id,
                        'role_id' => $role_id,
                    ];
                }
                DB::table('sys_role_menu')->where('role_id', $role_id)->delete();
                DB::table('sys_role_menu')->insertOrIgnore($add_datas);
            }else{
                DB::table('sys_role_menu')->where('role_id', $role_id)->delete();
            }
        });

        return success([],'添加成功');
    }

    public function update(SysRoleRequest $request, SysRole $model)
    {
        $params = $request->all();
        $id = $request->post('id');
        $role = $model->find($id);
        if(!$role){
            return error('数据不存在');
        }
        DB::transaction(function () use ($params, $role) {
            $role->update($params);
            $role_id = $role->id;
            $menuIds = $params['menuIds'];
            if(count($menuIds) > 0){
                foreach ($menuIds as $menu_id){
                    $add_datas[] = [
                        'menu_id' => $menu_id,
                        'role_id' => $role_id,
                    ];
                }
                DB::table('sys_role_menu')->where('role_id', $role_id)->delete();
                DB::table('sys_role_menu')->insertOrIgnore($add_datas);
            }else{
                DB::table('sys_role_menu')->where('role_id', $role_id)->delete();
            }
        });
        return success([],'更新成功');
    }

    public function destroy($ids, Request $request, SysRole $model){
        $id_arr = explode(',', $ids);
        DB::transaction(function () use ($model, $id_arr) {
            $model->destroy($id_arr);
            DB::table('sys_role_menu')->whereIn('role_id', $id_arr)->delete();
        });
        return success([],'删除成功');

    }
    public function updateDataScope(Request $request, SysRole $model){
        $params = $request->all();
        $id = $request->post('id');
        $role = $model->find($id);
        if(!$role){
            return error('数据不存在');
        }
        DB::transaction(function () use ($params, $role) {
            $role->update([
                'dept_check_strictly' => $params['dept_check_strictly'],
                'data_scope' => $params['data_scope']
            ]);
            $role_id = $role->id;
            $deptIds = $params['deptIds'];
            if(count($deptIds) > 0){
                foreach ($deptIds as $dept_id){
                    $add_datas[] = [
                        'dept_id' => $dept_id,
                        'role_id' => $role_id,
                    ];
                }
                DB::table('sys_role_dept')->where('role_id', $role_id)->delete();
                DB::table('sys_role_dept')->insertOrIgnore($add_datas);
            }else{
                DB::table('sys_role_dept')->where('role_id', $role_id)->delete();
            }
        });

        return success([],'更新成功');
    }
    public function changeStatus(Request $request, SysRole $model){
        $params = $request->all();
        $id = $request->post('role_id');
        $status = $request->post('status');

        $role = $model->find($id);
        if(!$model){
            return error('数据不存在');
        }
        $role->update([
            'status' => $status
        ]);
        return success([],'更新成功');

    }

}
